<?php
declare (strict_types = 1);
namespace app\index\model\user;

use think\Model;

/**
 * @mixin \think\Model
 */
class UserCar extends Model
{
    protected $pk = "id";

    /**
     * 获取信息
     * @param $where
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getInfoByWhere($where,$field="*"){
        $info = $this->where($where)->where(['is_delete'=>1])->field($field)->find();
        if($info){
            $info['car_image'] = $info['car_image'] ? config('add.web_url').$info['car_image'] : config('add.web_url').config('add.car_img');
            $info['driving_img'] = $info['driving_img'] ? config('add.web_url').$info['driving_img'] : '';
        }
        return $info;
    }

    /**
     * 获取信息
     * @param $id
     * @param $addWhere
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getInfoById($id,$addWhere = [],$field = '*'){
        $where = ['id'=>$id,'is_delete'=>1];
        return $this->where($where)->where($addWhere)->field($field)->find();
    }

    /**
     * 获取指定用户的车辆列表
     * @param $userId
     * @param string $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getUserCar($userId,$field = '*',$page=1,$limit=20){
        $authenticationStatus = [0=>'未认证',1=>'已认证'];
        $carImg = config('add.web_url').config('add.car_img');
        return $this->where(['user_id'=>$userId,'is_delete'=>1])->field($field)->page($page,$limit)
            ->select()->each(function ($item) use($authenticationStatus,$carImg){
                $item['authentication_name'] = $authenticationStatus[$item['is_authentication']];
                $item['car_image'] = $item['car_image'] ? config('add.web_url').$item['car_image'] : $carImg;
                return $item;
        })->toArray();
    }

    /**
     * 获取指定用户的车辆列表
     * @param $userId
     * @param string $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getUserCarList($userId,$field = '*',$page=1,$limit=20){
        $authenticationStatus = [0=>'未认证',1=>'已认证'];
        $carImg = config('add.web_url').config('add.car_img');
        $webUrl = config('add.web_url');
        $list = $this->where(['user_id'=>$userId,'is_delete'=>1])->field($field)->page($page,$limit)
            ->select()->each(function ($item) use($authenticationStatus,$carImg,$webUrl){
                $item['authentication_name'] = $authenticationStatus[$item['is_authentication']];
                $item['car_image'] = $item['car_image'] ? $webUrl.$item['car_image'] : $carImg;
                return $item;
            })->toArray();
        $count =  $this->where(['user_id'=>$userId,'is_delete'=>1])->count();
        return compact('count','list');
    }

    /**
     * 用户绑定汽车
     * @param $addData
     * @return int|string
     */
    function addUserCard($addData){
        $addData = [
            'user_id'=>$addData['user_id'],
            'car_brand'=>$addData['car_brand'],
            'car_model'=>$addData['car_model'],
            'car_type'=>isset($addData['car_type']) ? $addData['car_type'] : '',
            'car_number'=>$addData['car_number'],
            'car_travel_number'=>isset($addData['car_travel_number']) ? $addData['car_travel_number'] : '',
            'is_authentication'=>0,
            'car_image'=>isset($addData['car_image']) ? $addData['car_image'] : '',
            'create_time'=>date("Y-m-d H:i:s"),
            'update_time'=>date("Y-m-d H:i:s"),
            //'is_authentication'=>1,
        ];
        return $this->insert($addData);
    }

    /**
     * 修改绑定汽车
     * @param $id
     * @param $saveData
     * @return static
     */
    function updateUserCar($id,$saveData){
        return $this->where('id',$id)->update($saveData);
    }


}
